<?php

namespace app\controller\admin;

use app\model\AdminStatisticsPeople;
use app\model\AdminStaff;
use app\util\ReturnCode;
use think\Response;

class StatisticsPeople extends Base {

    /**
     * 获取时间点列表
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPeopleTimeList(): Response {
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        // 时间点
        $time = $this->request->get('time', date('Y-m-d'));
        $list = AdminStatisticsPeople::where('school_id','=', $school_id)
            ->where('statistics_time', '>=', strtotime($time.' 00:00:00'))
            ->where('statistics_time', '<=', strtotime($time.' 23:59:59'))->select();
        foreach ($list as $value) {
            $value['statistics_time'] = date('H:i', $value['statistics_time']);
        }
        // 实时
        $all_user = AdminStaff::where(['school_id'=>$school_id, 'type'=>AdminStaff::STUDENT, 'is_delete'=>0])->count();
        $out_user = AdminStaff::where(['school_id'=>$school_id, 'type'=>AdminStaff::STUDENT, 'is_delete'=>0, 'last_control_status'=>1])->count();
        $in_user = $all_user - $out_user;
        $data = [];
        $data["all_user"] = $all_user;
        $data["out_user"] = $out_user;
        $data["in_user"] = $in_user;
        return $this->buildSuccess([
            'list' => $list,
            'data' => $data
        ]);
    }

    /**
     * 获取已归未归学生数据
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPeopleStaffList(): Response {
        $statistics_people_id = $this->request->get('statistics_people_id');
        $statistics_people_type = $this->request->get('type'); // in out
        $dor_id = $this->request->get('dor_id', '');
        $dor_stu_id = $this->request->get('dor_stu_id', '');
        if (!$statistics_people_id) {
            return $this->buildFailed(ReturnCode::INVALID, '时间点ID不能为空');
        }
        $statistics_people = AdminStatisticsPeople::where('id','=', $statistics_people_id)->find();
        if(!$statistics_people) {
            return $this->buildSuccess([
                'list' => []
            ]);
        }
        $pieces = explode(":", $statistics_people->out_user_ids);
        $where = [
            ['s.type', '=', AdminStaff::STUDENT],
            ['s.status', '=', AdminStaff::AUDIT_PASS],
            ['s.is_delete', '=', 0]
        ];
        if($statistics_people_type == 'out') {
            array_push($where, ['s.id', 'in', $pieces]);
        } else if($statistics_people_type == 'in') {
            array_push($where, ['s.id', 'not in', $pieces]);
            array_push($where, ['s.school_id', '=', $statistics_people->school_id]);
        } else {
            return $this->buildFailed(ReturnCode::INVALID, '查询数据类型错误');
        }
        if ($dor_id) {
            array_push($where, ['ic.dormitory_id', '=', $dor_id]);
        }
        if ($dor_stu_id) {
            array_push($where, ['ic.student_dormitory_id', '=', $dor_stu_id]);
        }
        $list = AdminStaff::alias('s')
            ->leftJoin(['admin_staff_ic_card' => 'ic'], 'ic.staff_id=s.id and ic.is_delete = 0')
            ->field('s.id,s.school_id,s.student_no,s.name,s.sex,s.major,s.phone,s.create_time,s.status,s.entrance_time,s.graduate_time,s.face')
            ->group('s.id')
            ->with(['icCard'=>['student', 'dormitory']])
            ->where($where)->select();
        return $this->buildSuccess([
            'list' => $list
        ]);
    }

    /**
     * 获取已归未归学生数据（实时）
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPeopleStaffNowList(): Response {
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $statistics_people_type = $this->request->get('type'); // in out
        $dor_id = $this->request->get('dor_id', '');
        $dor_stu_id = $this->request->get('dor_stu_id', '');
        if (!$school_id) {
            return $this->buildFailed(ReturnCode::INVALID, '请选择学校');
        }
        $where = [
            ['s.type', '=', AdminStaff::STUDENT],
            ['s.is_delete', '=', 0],
            ['s.school_id', '=', $school_id]
        ];
        $list = AdminStaff::alias('s')->where($where);
        if($statistics_people_type == 'out') {
            $list->where('s.last_control_status', '=', 1);
        } else if($statistics_people_type == 'in') {
            $list->whereExp('s.last_control_status', ' = 0 or s.last_control_status is null');
        } else {
            return $this->buildFailed(ReturnCode::INVALID, '查询数据类型错误');
        }
        if ($dor_id) {
            $list->where('ic.dormitory_id', '=', $dor_id);
        }
        if ($dor_stu_id) {
            $list->where('ic.student_dormitory_id', '=', $dor_stu_id);
        }
        $list = $list
            ->leftJoin(['admin_staff_ic_card' => 'ic'], 'ic.staff_id=s.id and ic.is_delete = 0')
            ->field('s.id,s.school_id,s.student_no,s.name,s.sex,s.major,s.phone,s.create_time,s.status,s.entrance_time,s.graduate_time,s.face')
            ->group('s.id')
            ->with(['icCard'=>['student', 'dormitory']])
            ->select();
        return $this->buildSuccess([
            'list' => $list
        ]);
    }
}